package com.rh.i_mes.controller.ums;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rh.i_mes.common.model.PageResult;
import com.rh.i_mes.common.model.Result;
import com.rh.i_mes.model.UmsRolePer;
import com.rh.i_mes.service.IUmsRolePerService;
import com.rh.i_mes.util.EasyPoiExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户权限关联表
 *
 * @author hqb
 * @date 2020-09-12 16:31:21
 */
@Slf4j
@CrossOrigin
@RestController
@Api(tags = "用户权限关联表")
@RequestMapping("/ums")
public class UmsRolePerController {
    @Autowired
    private IUmsRolePerService umsRolePerService;

    /**
     * 列表
     */
    @ApiOperation(value = "查询列表")
    @PostMapping("/umsRolePer/list")
    public Result<PageResult> list(@RequestBody Map<String, Object> params) {
        Page<UmsRolePer> list= umsRolePerService.findList(params);
        return Result.succeed(PageResult.restPage(list),"查询成功");
    }

    /**
     * 查询
     */
    @ApiOperation(value = "查询")
    @PostMapping("/umsRolePer/sel/{id}")
    public Result findUserById(@PathVariable Long id) {
        UmsRolePer model = umsRolePerService.getById(id);
        return Result.succeed(model, "查询成功");
    }

    /**
     * 新增or更新
     */
   /* @ApiOperation(value = "未分配-》已分配")
    @PostMapping("/umsRolePer/save")
    public Result save(@RequestBody UmsRolePer umsRolePer) {
        umsRolePerService.saveOrUpdate(umsRolePer);
        return Result.succeed("保存成功");
    }
    @ApiOperation(value = "已分配-》未分配")
    @PostMapping("/umsRolePer/remove")
    public Result edit(@RequestBody UmsRolePer umsRolePer) {
        Long perId = umsRolePer.getPerId();
        Long roleId = umsRolePer.getRoleId();
        umsRolePerService.remove(new QueryWrapper<UmsRolePer>()
                .eq("per_id",perId)
                .eq("role_id",roleId)
        );
        return Result.succeed("移除成功");
    }*/
    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @PostMapping("/umsRolePer/del/{id}")
    public Result delete(@PathVariable Long id) {
        umsRolePerService.removeById(id);
        return Result.succeed("删除成功");
    }
    
    /**
     * 导入
     */
    @ApiOperation(value = "导入")
    @PostMapping("/umsRolePer/leadIn")
    public  Result leadIn(MultipartFile excel, String cuId) throws Exception {
        int rowNum = 0;
        if (!excel.isEmpty()) {
            List<UmsRolePer> list = EasyPoiExcelUtil.importExcel(excel, 1, 1, UmsRolePer.class);
            rowNum = list.size();
            if (rowNum > 0) {
                //无该用户信息
                list.forEach(u -> {
                        umsRolePerService.save(u);
                });
                return Result.succeed("成功导入信息"+rowNum+"行数据");
            }
        }
        return Result.failed("导入失败");
    }
    
    /**
     * 导出
     */
    @ApiOperation(value = "导出")
    @PostMapping("/umsRolePer/leadOut")
    public void leadOut(String cuId, HttpServletResponse response) throws IOException {
        List<UmsRolePer> umsRolePers =new ArrayList<>();
        List<UmsRolePer> umsRolePerList = umsRolePerService.list(new QueryWrapper<UmsRolePer>().eq("cu_id", cuId));
        if (umsRolePerList.isEmpty()) {umsRolePers.add(umsRolePerService.getById(0)); } else {
            for (UmsRolePer umsRolePer : umsRolePerList) {
                umsRolePers.add(umsRolePer);
            }
        }
        //导出操作
        EasyPoiExcelUtil.exportExcel(umsRolePers, "用户权限关联表导出", "用户权限关联表导出", UmsRolePer.class, "umsRolePer.xls", response);

    }


}
